package com.windbell.rest.services

import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.beans.factory.annotation.Autowired
import edu.fudan.mylang.pf.IObjectFactory
import java.util.Collection
import com.windbell.rest.server.service.risk.RiskCategoryMapper
import com.windbell.rest.shared.risk.UIRiskCategory
import windbell.domain.risk.RiskCategory
import com.windbell.rest.shared.risk.UIRiskSubCategory
import com.windbell.rest.server.service.risk.RiskSubCategoryMapper
import com.windbell.rest.server.service.risk.RiskCommonIssueMapper
import windbell.domain.risk.RiskSubCategory
import com.windbell.rest.shared.risk.UIRiskCommonIssue
import windbell.domain.risk.RiskCommonIssue
import windbell.domain.risk.RiskCategoryDAO
import windbell.domain.risk.RiskSubCategoryDAO
import windbell.domain.risk.RiskCommonIssueDAO
import com.windbell.rest.shared.risk.UIRiskCategoryCriteria
import windbell.domain.risk.RiskCategoryCriteria
import com.windbell.rest.server.service.risk.RiskCategoryCriteriaMapper
import com.windbell.rest.shared.risk.UIRiskCheck
import windbell.domain.risk.RiskCheck
import com.windbell.rest.server.service.risk.RiskCheckCriteriaMapper
import com.windbell.rest.server.service.risk.RiskCheckMapper
import com.windbell.rest.shared.risk.UIRiskCheckItem
import windbell.domain.risk.RiskCheckItemCriteria
import com.windbell.rest.shared.risk.UIRiskCheckItemCriteria
import com.windbell.rest.server.service.risk.RiskCheckItemCriteriaMapper
import com.windbell.rest.server.service.risk.RiskCheckItemMapper
import windbell.domain.risk.RiskType
import windbell.domain.risk.RiskLevel
import windbell.domain.risk.RiskCause
import windbell.domain.risk.RiskFixType
import com.windbell.rest.shared.risk.UIRiskReport
import windbell.domain.risk.RiskCheckItem
import com.windbell.rest.server.service.risk.RiskReportCriteriaMapper
import com.windbell.rest.server.service.risk.RiskReportMapper
import com.windbell.rest.shared.risk.UIRiskType
import com.windbell.rest.server.service.risk.RiskTypeMapper
import com.windbell.rest.server.service.risk.RiskLevelMapper
import com.windbell.rest.server.service.risk.RiskCauseMapper
import com.windbell.rest.server.service.risk.RiskFixTypeMapper
import com.windbell.rest.shared.risk.UIRiskLevel
import com.windbell.rest.shared.risk.UIRiskCause
import com.windbell.rest.shared.risk.UIRiskFixType
import windbell.domain.risk.RiskReport
import com.windbell.rest.shared.risk.UIRiskCheckCriteria
import windbell.domain.risk.RiskCheckCriteria
import windbell.domain.risk.RiskCheckTarget
import com.windbell.rest.shared.risk.UIRiskCheckTarget
import com.windbell.rest.server.service.risk.RiskCheckTargetMapper
import com.windbell.rest.shared.risk.RestRiskCheckStatus
import com.windbell.rest.shared.risk.RestRiskCheckItemStatus
import windbell.domain.risk.RiskCheckStatus
import windbell.domain.risk.RiskCheckItemStatus

@Service
@Transactional
class CCRiskService {
	@Autowired private IObjectFactory entities
	@Autowired private RiskCategoryMapper riskCategoryMapper
	@Autowired private RiskSubCategoryMapper riskSubCategoryMapper
	@Autowired private RiskCommonIssueMapper commonIssueMapper
	@Autowired RiskCategoryDAO riskCategoryDAO
	@Autowired RiskSubCategoryDAO riskSubCategoryDAO
	@Autowired RiskCommonIssueDAO riskCommonIssueDAO
	@Autowired RiskCategoryCriteriaMapper riskCategoryCriteriaMapper
	
	@Autowired RiskCheckCriteriaMapper riskCheckCriteriaMapper
	@Autowired private RiskCheckMapper riskCheckMapper
	
	@Autowired private RiskCheckItemCriteriaMapper riskCheckItemCriteriaMapper
	@Autowired private RiskCheckItemMapper riskCheckItemMapper
	@Autowired private RiskReportCriteriaMapper riskReportCriteriaMapper
	@Autowired private RiskReportMapper riskReportMapper
	
	@Autowired private RiskTypeMapper riskTypeMapper
	@Autowired private RiskLevelMapper riskLevelMapper
	@Autowired private RiskCauseMapper riskCauseMapper
	@Autowired private RiskFixTypeMapper riskFixTypeMapper
	@Autowired
   	private TransactionalRunner transactionalRunner
   	@Autowired
   	private RiskCheckTargetMapper riskCheckTargetMapper
   	
   	
	
	// 获取隐患列表
	def Collection<UIRiskCategory> getRiskCategoryList(){
		entities.all(RiskCategory).^map[riskCategoryMapper.transform(it)].toList
	}
	// 获取特定隐患大类下隐患小类列表
	def Collection<UIRiskSubCategory> getRiskSubCategoryList(Long riskCategoryId){
		entities.get(RiskCategory, riskCategoryId).subCategory.^map[riskSubCategoryMapper.transform(it)].toList
	}
	// 获取特定隐患小类下隐患不合格项列表
	def Collection<UIRiskCommonIssue> getRiskCommonIssueList(Long riskSubCategoryId){
		entities.get(RiskSubCategory, riskSubCategoryId).commonIssues.^map[commonIssueMapper.transform(it)].toList
	}
	
	// 创建隐患大类
	def UIRiskCategory createRiskCategory(UIRiskCategory riskCategory) {
		riskCategoryMapper.transform(
			riskCategoryDAO.create(riskCategory.code)[
				riskCategoryMapper.transform(riskCategory,it)
			]
		)
	}
	// 创建隐患小类
	def UIRiskSubCategory createRiskSubCategory(Long riskCategoryId, UIRiskSubCategory riskSubCategory) {
		riskSubCategoryMapper.transform(
			riskSubCategoryDAO.create(entities.get(RiskCategory, riskCategoryId))[
				riskSubCategoryMapper.transform(riskSubCategory,it)
			]
		)
	}
	// 创建隐患不合格项
	def UIRiskCommonIssue createRiskCommonIssue(Long riskSubCategoryId, UIRiskCommonIssue riskCommonIssue) {
		commonIssueMapper.transform(
			riskCommonIssueDAO.create(entities.get(RiskSubCategory, riskSubCategoryId))[
				commonIssueMapper.transform(riskCommonIssue,it)
			]
		)
	}

	// 更新隐患大类
	def UIRiskCategory updateRiskCategory(UIRiskCategory riskCategory){
		riskCategoryMapper.transform(
			riskCategoryMapper.transform(riskCategory, entities.get(RiskCategory, riskCategory.id))
		)
	}
	// 更新隐患小类
	def UIRiskSubCategory updateRiskSubCategory(UIRiskSubCategory riskSubCategory){
		riskSubCategoryMapper.transform(
			riskSubCategoryMapper.transform(riskSubCategory, entities.get(RiskSubCategory, riskSubCategory.id))
		)
	}
	// 更新隐患不合格项
	def UIRiskCommonIssue updateCommonIssue(UIRiskCommonIssue riskCommonIssue){
		commonIssueMapper.transform(
			commonIssueMapper.transform(riskCommonIssue, entities.get(RiskCommonIssue, riskCommonIssue.id))
		)
	}
	
	// 删除隐患大类
	def deleteRiskCategory(Long riskCategoryId) {
		entities.delete(entities.get(RiskCategory, riskCategoryId))
	}
	// 删除隐患小类
	def deleteRiskSubCategory(Long riskSubCategoryId) {
		entities.delete(entities.get(RiskSubCategory, riskSubCategoryId))
	}
	// 删除隐患不合格项
	def deleteRiskCommonIssue(Long riskCommonIssueId) {
		entities.delete(entities.get(RiskCommonIssue, riskCommonIssueId))
	}
	
	def PagedResult<UIRiskCategory> getRiskCategoryList(UIRiskCategoryCriteria criteria) {
		val c = (entities.createQueryCriteria(RiskCategoryCriteria))=>[
			riskCategoryCriteriaMapper.transform(criteria, it)
		]
		
		if (criteria.info!=null)
			c.info = "%"+criteria.info+"%"
		
		c.start = criteria.skip
		c.limit = criteria.pagesize
		
		PagedResults.listAndTransform(c)[
			riskCategoryMapper.apply(it)
		]
	}
	
	/*************************************
	 * 隐患排查任务相关
	 *************************************/
	 // 隐患排查列表
	def Collection<UIRiskCheck> riskCheckList() {
		entities.all(RiskCheck).^map[riskCheckMapper.transform(it)].toList
	}
	
	// 隐患排查详情
	def UIRiskCheck riskCheckDetail(Long riskCheckId) {
		riskCheckMapper.transform(entities.get(RiskCheck, riskCheckId))
	}
	
	// 隐患排查检查条目列表
	def PagedResult<UIRiskCheckItem> getRiskCheckItemList(UIRiskCheckItemCriteria criteria) {
		val c = (entities.createQueryCriteria(RiskCheckItemCriteria))=>[
			riskCheckItemCriteriaMapper.transform(criteria, it)
		]
		
		if (criteria.expert!=null)
			c.expert.name = "%"+criteria.expert.name+"%"
		
		c.start = criteria.skip
		c.limit = criteria.pagesize
		
		PagedResults.listAndTransform(c)[
			riskCheckItemMapper.apply(it)
		]
	}
	
	// 隐患排查列表
	def PagedResult<UIRiskCheck> getRiskCheckList(UIRiskCheckCriteria criteria) {
		val c = (entities.createQueryCriteria(RiskCheckCriteria))=>[
			riskCheckCriteriaMapper.transform(criteria, it)
		]
		
		PagedResults.listAndTransform(c)[
			riskCheckMapper.apply(it)
		]

	
	}
	
	// 隐患排查详情
	def UIRiskCheckItem riskCheckItemDetail(Long riskCheckItemId) {
		riskCheckItemMapper.transform(entities.get(RiskCheckItem, riskCheckItemId))
	}
	
	// 新建隐患报告
	def UIRiskReport createRiskReport (Long checkItemId, UIRiskReport riskReport) {
		println(entities.get(RiskCheckItem, checkItemId))
		riskReportMapper.transform(
			entities.get(RiskCheckItem, checkItemId).createReport[
			riskReportMapper.transform(riskReport,it)
		])
	}
	
	// 隐患报告列表
	def Collection<UIRiskReport> riskReportList () {
		entities.all(typeof(RiskReport)).^map[riskReportMapper.transform(it)].toList
	}
	// 隐患报告详情
	def UIRiskReport riskReportDetail (Long riskReportId) {
		riskReportMapper.transform(entities.get(RiskReport, riskReportId))
	}
	
	
	//隐患类型
	def Collection<UIRiskType> getRiskType() {
		entities.all(typeof(RiskType)).^map[riskTypeMapper.transform(it)].toList
	}
	
	//隐患级别
	def Collection<UIRiskLevel> getRiskLevel() {
		entities.all(typeof(RiskLevel)).^map[riskLevelMapper.transform(it)].toList
	}
	
	//隐患来源
	def Collection<UIRiskCause> getRiskCause() {
		entities.all(typeof(RiskCause)).^map[riskCauseMapper.transform(it)].toList
	}
	
	//整改类型
	def Collection<UIRiskFixType> getRiskFixType() {
		entities.all(typeof(RiskFixType)).^map[riskFixTypeMapper.transform(it)].toList
	}
	
	//排查单位列表
	def Collection<UIRiskCheckTarget> getCheckTarget() {
		entities.all(typeof(RiskCheckTarget)).^map[riskCheckTargetMapper.transform(it)].toList
	}
	
	
	//隐患排查状态列表
	def Collection<RestRiskCheckStatus> riskCheckStatusList() {
		RiskCheckStatus.values.map[
			RestRiskCheckStatus.valueOf(it.name)
		]
	}
	
	//隐患排查检查条目状态列表
	def Collection<RestRiskCheckItemStatus> riskCheckItemStatusList() {
		RiskCheckItemStatus.values.map[
			RestRiskCheckItemStatus.valueOf(it.name)
		]
	}
	
	
}